package logic

import (
	"context"
	"errors"
	"shop_srv/model/mysql"
	"shop_srv/model/redisinit"

	"shop_srv/internal/svc"
	"shop_srv/shop"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddLogic {
	return &AddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddLogic) Add(in *shop.AddRequest) (*shop.AddResponse, error) {
	// todo: add your logic here and delete this line

	s := mysql.Shop{}
	err := s.Info(in.ShopId)
	if err != nil {
		logx.Error("商品不存在")
		return nil, errors.New("商品不存在")
	}
	if s.ID == 0 {
		logx.Error("商品不存在")
		return nil, errors.New("商品不存在")
	}
	if s.ShopStock < in.Num {
		logx.Error("库存不足")
		return nil, errors.New("库存不足")
	}
	redisinit.CartAdd(in.UserId, in.ShopId, s.ShopStock, s.ShopPrice, s.ShopStatus, s.ShopName)
	return &shop.AddResponse{}, nil
}
